package com.ruoyi.platform.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.UserComplaintsHandle;
import com.ruoyi.platform.service.IUserComplaintsHandleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户售后处理Controller
 */
@RestController
@RequestMapping("/platform/complaints_handle")
public class UserComplaintsHandleController extends BaseController {
    @Autowired
    private IUserComplaintsHandleService userComplaintsHandleService;

    /**
     * 查询售后审核列表（只显示创建时间>24小时的投诉）
     */
    @PreAuthorize("@ss.hasPermi('platform:complaints_handle:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserComplaintsHandle userComplaintsHandle) {
        startPage();
        List<UserComplaintsHandle> list = userComplaintsHandleService.selectComplaintsAuditList(userComplaintsHandle);
        return getDataTable(list);
    }

    /**
     * 导出售后审核列表
     */
    @PreAuthorize("@ss.hasPermi('platform:complaints_handle:export')")
    @Log(title = "售后审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserComplaintsHandle userComplaintsHandle) {
        List<UserComplaintsHandle> list = userComplaintsHandleService.selectComplaintsAuditList(userComplaintsHandle);
        ExcelUtil<UserComplaintsHandle> util = new ExcelUtil<>(UserComplaintsHandle.class);
        util.exportExcel(response, list, "售后审核数据");
    }

    /**
     * 确认退款（状态更新为2-已完成）
     */
    @PreAuthorize("@ss.hasPermi('platform:complaints_handle:handle')")
    @Log(title = "售后处理", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmRefund")
    public AjaxResult confirmRefund(@RequestBody UserComplaintsHandle handle) {
        handle.setStatus(2L); // 已完成
        return toAjax(userComplaintsHandleService.handleComplaint(handle));
    }

    /**
     * 驳回投诉（状态更新为3-已驳回）
     */
    @PreAuthorize("@ss.hasPermi('platform:complaints_handle:handle')")
    @Log(title = "售后处理", businessType = BusinessType.UPDATE)
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody UserComplaintsHandle handle) {
        handle.setStatus(3L); // 已驳回
        return toAjax(userComplaintsHandleService.handleComplaint(handle));
    }
}